package demo1;

public class MyLinkedList {
    static class Node {
        private int val;
        private Node next;

        public Node(int val) {
            this.val = val;
        }
    }
    private Node head;//引用变量  用来引用节点对象

    //创建链表
    public void creatList() {
        MyLinkedList.Node node1 = new MyLinkedList.Node(11);
        MyLinkedList.Node node2 = new MyLinkedList.Node(11);
        MyLinkedList.Node node3 = new MyLinkedList.Node(11);
        MyLinkedList.Node node4 = new MyLinkedList.Node(11);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }


    //打印链表
    public void display(){
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //头插法
    public void addFirst(int data){
        MyLinkedList.Node newNode = new MyLinkedList.Node(data);
        newNode.next = this.head;
        this.head = newNode;
    }
    //尾插法
    public void addLast(int data){
        MyLinkedList.Node cur = this.head;
        MyLinkedList.Node newNode = new MyLinkedList.Node(data);
        if (head == null) {
            head = newNode;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newNode;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) throws CheckIndexException{
        if (index < 0 || index > size()) {
            throw new CheckIndexException("下标错误");
        }
        //下标为0时  头插
        if (index == 0) {
            addFirst(data);
        }
        //下标等于size() - 1  尾插
        if (index == (size() - 1)) {
            addLast(data);
        }
        MyLinkedList.Node cur = this.head;
        MyLinkedList.Node newNode = new MyLinkedList.Node(data);
        //让cur先走到要插入的位置前一个
        int i = index - 1;
        while (i-- > 0) {
            cur = cur.next;
        }
        newNode.next = cur.next;
        cur.next = newNode;

    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        MyLinkedList.Node cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //判断头结点是不是要删除的key
        if (head.val == key) {
            head = head.next;
        }
        MyLinkedList.Node cur = this.head;

        //第一种写法
        while (cur.next != null) {
            if (cur.next.val == key) {
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }

        //第二种写法
        MyLinkedList.Node del = this.head.next;
        while (del != null) {
            if (del.val == key) {
                cur.next = del.next;
                break;
            }
            del = del.next;
            cur = cur.next;
        }

    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        MyLinkedList.Node cur = this.head;
        MyLinkedList.Node del = this.head.next;

       /* while (head.val == key) {
            head = head.next;
        }*/

        while (del != null) {
            if (del.val == key) {
                cur.next = del.next;
            } else {
                cur = cur.next;
            }
            del = del.next;
        }

        //最后判断头结点的值是不是key
        if (this.head.val == key) {
            this.head = this.head.next;
        }

    }

    //得到单链表的长度
    public int size(){
        MyLinkedList.Node cur = this.head;
        int count = 0;
        while (cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    public void clear(){
        MyLinkedList.Node cur = this.head.next;
        while (cur != null) {
            head.next = null;
            head = cur;
            cur = cur.next;
        }
    }



}
